package cn.flying.cloud.system.param.service.impl;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.system.param.entity.SysParamInfo;
import cn.flying.cloud.system.param.mapper.SysParamInfoMapper;
import cn.flying.cloud.system.param.service.SysParamInfoService;

/**
 * 系统参数信息表 服务实现类
 *
 * @author admin
 * @since 2025-04-16 23:22:57
 */
@Service
public class SysParamInfoServiceImpl extends ServiceImpl<SysParamInfoMapper, SysParamInfo> implements SysParamInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询总数
     *
     * @param sysParamInfo
     * @return
     */
    @Override
    public long countObj(SysParamInfo sysParamInfo) {
        return this.count(getLambdaQueryWrapper(sysParamInfo));
    }

    /**
     * 分页查询系统参数信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysParamInfo> getPageList(PageRequest<SysParamInfo> request) {
        SysParamInfo param = request.getParam();
        LambdaQueryWrapper<SysParamInfo> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        Page<SysParamInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysParamInfo> getLambdaQueryWrapper(SysParamInfo param) {
        LambdaQueryWrapper<SysParamInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(SysParamInfo::getStatus, param.getStatus());
            }
            if (Ft.String.isNotBlank(param.getParamKey())) {
                wrapper.eq(SysParamInfo::getParamKey, param.getParamKey());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询系统参数信息表数据
     *
     * @param sysParamInfo
     * @return
     */
    @Override
    public List<SysParamInfo> getList(SysParamInfo sysParamInfo) {
        LambdaQueryWrapper<SysParamInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.list(wrapper);
    }

    /**
     * 查询系统参数信息表详情数据
     *
     * @param sysParamInfo
     * @return
     */
    @Override
    public SysParamInfo getOneObj(SysParamInfo sysParamInfo) {
        LambdaQueryWrapper<SysParamInfo> wrapper = getLambdaQueryWrapper(sysParamInfo);
        return this.getOne(wrapper);
    }

    /**
     * 根据Id查询系统参数信息表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public SysParamInfo getObjById(Serializable id) {
        return this.getById(id);
    }

    /**
     * 新增系统参数信息表数据
     *
     * @param sysParamInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysParamInfo sysParamInfo) {
        sysParamInfo.setTenantId(Ft.Local.TenantId());
        return this.save(sysParamInfo);
    }

    /**
     * 更新系统参数信息表数据
     *
     * @param sysParamInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysParamInfo sysParamInfo) {
        return this.saveOrUpdate(sysParamInfo);
    }

    /**
     * 删除系统参数信息表数据
     *
     * @param sysParamInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysParamInfo sysParamInfo) {
        LambdaQueryWrapper<SysParamInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除系统参数信息表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
